package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSON;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.result.ResultModel;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //         1.如果前端传递中文时，可以会出现乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //        2.要得到前端的数据
        String cuisineId = req.getParameter("cuisine");
        String name = req.getParameter("productName");
        String imgHref = req.getParameter("imgHref");
        String content = req.getParameter("content");


        //        3.封装model
        ProductModel productModel = new ProductModel();
        productModel.setName(name);
        productModel.setCuisineId(Tool.stringToInt(cuisineId));
        productModel.setContent(content);
        productModel.setImgHref(imgHref);


//        4.调用服务逻辑层
        ProductService productService = new ProductServiceImpl();

        //        5.得到统一的返回值对象
        ResultModel resultModel = productService.add(productModel);
        //       6.将数据返回给前端页面
        resp.getWriter().println(JSON.toJSONString(resultModel));
    }


}
